import xadmin
from .models import Goods, GoodsCategory, Banner, Brand, HotSearchWords, DiscountPackage, SearchAd, \
    GoodsBannerImage, GoodsDescImage, GoodsAttribute
# Specification, GoodsBannerImage, GoodsDescImage, GoodsColor, GoodsAttribute
from xadmin import views


class BaseSetting(object):
    enable_themes = 'True'
    use_bootswatch = 'True'


class GlobalSettings(object):
    site_title = '商城后台管理系统'
    site_footer = '创立美科技'
    menu_style = 'accordion'


class GoodsAdmin(object):
    """商品"""
    # 需要显示的字段
    list_display = ['goods_sn', 'name', 'goods_front_image', 'sold_num', 'serve', 'ship_address',
                    'goods_status', 'is_hot', 'is_highpoint', 'is_index',
                    'ship_free', ]
    # 可以用来做搜索条件的字段
    search_fields = ['name']
    # 设置可以直接在列表中更改的字段
    list_editable = ["is_hot", ]
    # 用时间格式做过滤条件的字段
    list_filter = ["name", "serve", "is_hot", "create_time", "category", ]
    # 在编辑页面隐藏的字段
    exclude = ["sold_num", "views_amount"]
    style_fields = {'relevance_goods': 'm2m_transfer'}  # m2m_transfer就是多对多穿梭框样式

    # style_fields = {"goods_desc": "ueditor"}
    class GoodsAttributeInline(object):
        model = GoodsAttribute
        exclude = ["create_time"]
        extra = 1
        style = 'tab'

    class GoodsBannerImageInline(object):
        model = GoodsBannerImage
        exclude = ["create_time"]
        extra = 1
        style = 'tab'

    class GoodsDescImageInline(object):
        model = GoodsDescImage
        exclude = ["create_time"]
        extra = 1
        style = 'tab'

    inlines = [GoodsAttributeInline, GoodsBannerImageInline, GoodsDescImageInline]


class GoodsColorAdmin(object):
    """商品颜色"""
    list_display = ["id", "color"]
    hidden_menu = True  # 设置不显示此模块菜单


class SpecificationAdmin(object):
    """商品规格"""
    list_display = ["id", "size"]


class GoodsCategoryAdmin(object):
    """商品分类"""
    list_display = ["name", "create_time"]
    list_filter = ["name", ]
    search_fields = ['name', ]


class BrandAdmin(object):
    """品牌"""
    list_display = ["name", "create_time"]
    list_filter = ["name", ]
    search_fields = ['name', ]


class BannerGoodsAdmin(object):
    """轮播"""
    list_display = ["goods", "image", ]


class DiscountPackageAdmin(object):
    """优惠套餐"""
    list_display = ["name", "image", "saved_at_most", "goods", "start_time", "end_time"]
    style_fields = {'goods': 'm2m_transfer'}  # m2m_transfer就是多对多穿梭框样式


class HotSearchAdmin(object):
    """热搜词"""
    list_display = ["keywords", "index", "create_time"]


class SearchAdAdmin(object):
    """搜索"""
    list_display = ["title", "image",]


xadmin.site.register(views.CommAdminView, GlobalSettings)
xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(Goods, GoodsAdmin)
xadmin.site.register(GoodsCategory, GoodsCategoryAdmin)
xadmin.site.register(Banner, BannerGoodsAdmin)
xadmin.site.register(Brand, BrandAdmin)
xadmin.site.register(DiscountPackage, DiscountPackageAdmin)
xadmin.site.register(HotSearchWords, HotSearchAdmin)
xadmin.site.register(SearchAd, SearchAdAdmin)
